package fy.hr.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;

import com.soa.eis.adapter.framework.message.IMsgObject;
import com.soa.eis.adapter.framework.message.impl.GroupRecord;
import com.soa.eis.adapter.framework.requester.IServiceRequester;
import com.soa.eis.adapter.framework.requester.impl.BaseServiceRequester;
import com.soa.eis.adapter.framework.utils.log.LogUtil;

import fy.config.FyConfig;
import fy.hr.bean.PersonInfoBean;
import fy.hr.bean.PersonInfoByCodeBean;
import fy.util.DBUtil;

public class MQPersonInfoDao {
	private String[] codes;
	private static Log log = LogFactory.getLog(MQPersonInfoDao.class);
	// private List<GroupRecord> record2 ;
	IMsgObject respMo = null;
	PersonInfoByCodeBean pbc = new PersonInfoByCodeBean();
private final static String chuchaiSQL = "select tab1.psncode, tab1.psnname,tab3.jobname from bd_psndoc tab1 join bd_deptdoc tab2 on tab1.pk_deptdoc=tab2.pk_deptdoc join om_job tab3 on tab1.pk_om_job=tab3.pk_om_job left join tbm_psndoc p on tab1.pk_psndoc = p.pk_psndoc where  to_date(p.enddate,'yyyy-mm-dd') > sysdate and psnclscope=0 ";
	//private final static String chuchaiSQL = "select tab1.psncode, tab1.psnname, tab2.deptname,tab3.jobname, p.pk_tbm_psndoc, tab1.pk_corp from bd_psndoc tab1 join bd_deptdoc tab2 on tab1.pk_deptdoc=tab2.pk_deptdoc join om_job tab3 on tab1.pk_om_job=tab3.pk_om_job left join tbm_psndoc p on tab1.pk_psndoc = p.pk_psndoc where  to_date(p.enddate,'yyyy-mm-dd') > sysdate and psnclscope=0 ";
	public JSONObject findPersonInfo() throws Exception {
		Connection conn = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		JSONObject jo = new JSONObject();
		try {
			conn = DBUtil.connect(FyConfig.getEHR_DB_CONFIG());

			st = conn.prepareStatement(getSQL());
			setParams(st);
			rs = st.executeQuery();

			while (rs.next()) {
				
				pbc.setJobNum(rs.getString(1));
				pbc.setName(rs.getString(2));
				pbc.setJob(rs.getString(3));
		/*		PersonInfoBean pib = new PersonInfoBean();
				pib.setCode(rs.getString(1));
				pib.setName(rs.getString(2));
				pib.setDep(rs.getString(3));
				pib.setPost(rs.getString(4));
				String pk_tbm_psndoc = rs.getString(5);
				pib.setExistPk_tbm_psndoc((pk_tbm_psndoc == null
						|| "".equals(pk_tbm_psndoc) ? false : true));
				pib.setPk_corp(rs.getString(6));
				if (this.needTime) {
					pib.setDate(rs.getString(7));
				}
				if(this.newAway){
					pib.setBasgroupdef23(groupRecord.getFieldValue("basgroupdef23"));
					pib.setId(groupRecord.getFieldValue("id"));
					if(code==0){
					pib.setMobile(groupRecord.getFieldValue("mobile"));}
				}*/
				jo.put(pbc.getID(), pbc.getJSONObject());
			
			//	jo.put(pib.getID(), pib.getJSONObject());
			}

		} catch (Exception e) {
			log.error("查找人员信息出错", e);
			e.printStackTrace();
		} finally {
			DBUtil.close(rs);
			DBUtil.close(st);
			DBUtil.close(conn);
		}

		return jo;
	}

	public void setCodes(String[] codes) {
		this.codes = codes;
	}

	private String getSQL() {
		StringBuilder sb = new StringBuilder();

		sb.append(chuchaiSQL);

		for (int i = 0, length = this.codes.length; i < length; i++) {
			if (i == 0) {
				sb.append(" and ( tab1.psncode=? ");
			} else {
				sb.append(" or tab1.psncode=? ");
			}
		}
		sb.append(" ) ");

		return sb.toString();
	}

	private void setParams(PreparedStatement st) throws Exception {
		for (int i = 0, length = this.codes.length; i < length; i++) {
			st.setString(i + 1, this.codes[i]);
		}
	}

/*	public void setNeedTime(boolean needTime) {
		this.needTime = needTime;
	}
*/
	/*	try {
			JSONObject jo = new JSONObject();
			IServiceRequester Requester = BaseServiceRequester.getInstance();
			// 获取服务调用者实例
			for (int i = 0, length = this.codes.length; i < length; i++) {
				// System.out.println("~~~~~~~~~~~~~~~~"+codes.length+codes[i]);
				// System.out.println("~~~~~~~~~~~~~~~~"+codes.length+codes[i]);
			
				// 定义请求报文对象
				IMsgObject reqMo = new MsgObject(IMsgObject.MOType.initSR);
				String SourceSysID = "06004"; // 请求方(发布方) 系统号
				String ServiceID = "06003000000008";// 响应方(发布方) 服务号
				// 获取系统时间
				Date date = new Date();
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
				String format = simpleDateFormat.format(date);
				// 对请求报文对象进行设置
				reqMo.setSourceSysID(SourceSysID); // 设置报文 请求方系统号
				reqMo.setServiceID(ServiceID); // 设置报文 提供方服务号
				
			//	reqMo.setSerialNO(format.substring(0, 8) + SourceSysID + "00000");// 设置报文流水号,第三段表示今天发送的第几的一次报文
				reqMo.setSerialNO(format.substring(0, 8) + SourceSysID + String.format("%06d", ++RequestNoTimerTask.requestNo));// 设置报文流水号,第三段表示今天发送的第几的一次报文
		

		
				reqMo.setServiceDateTime(format);
				IMsgObject reqMo= MqReqXml.add("06004", "06003000000008", codes[i]);
				//reqMo.setReqValue("string",codes[i]);
			// reqMo.setReqValue("string", "084655");
				//reqMo.setReqValue("string", codes[i]);
				// 打印请求报文对象
				LogUtil.getInstance().info("reqMo:" + reqMo);
				//System.out.println("reqMo:"+reqMo);
				respMo = Requester.execute(reqMo);
				//System.out.println("" + respMo);

				List<GroupRecord> record2 = respMo.getResGroupRecord("return");
				for (GroupRecord groupRecord : record2) {
					if (groupRecord.getFieldValue("psncode") == null) {
						//System.out.println("psncode is null");
						log.info("psncode is null");
					} else {
						// System.out.println("~~~~~~~~~~~~~~~~"+codes.length+codes[1]);

						// for(int i=0;codes.length>i;i++){
						// pbc.setJobNum(userCode);
						//System.out.println("~~~~~~~~~~~~~~~~" + groupRecord.getFieldValue("psncode"));
						pbc.setJobNum(groupRecord.getFieldValue("psncode"));
						pbc.setName(groupRecord.getFieldValue("psnname"));
						pbc.setJob(groupRecord.getFieldValue("jobname"));
						// System.out.println("psncode:"+groupRecord.getFieldValue("psncode")+"psnname:"+groupRecord.getFieldValue("psnname")+"jobname"+groupRecord.getFieldValue("jobname"));
						// jo.put(groupRecord.getFieldValue("psncode"),
						// pbc.getJSONObject());
						// }
						jo.put(pbc.getID(), pbc.getJSONObject());

					}
				}

			}
			return jo;
		} catch (Exception e) {
			log.error("~~~~~~~~~~~~~~~", e);
		}
		return null;

	}

	public void setCodes(String[] codes) {
		this.codes = codes;
	}
	*/

}
